#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author:liang futing
#@time :2019/4/10 16:07
import re
from django import forms
from django.contrib.auth import get_user_model
from .models import Structure,Menu

User=get_user_model()

class LoginForm(forms.Form):
    username=forms.CharField(required=True,error_messages={'requeired':'请填写用户名'})
    password = forms.CharField(required=True, error_messages={'requeired': '请填写密码'})

class StructureForm(forms.ModelForm):
    class Meta:
        model=Structure
        fields=['type','name','parent']

class UserCreateForm(forms.ModelForm):
    password=forms.CharField(
        required=True,
        min_length=6,
        max_length=20,
        error_messages={
            'required':'密码不能为空',
            'min_length':'密码长度最少6位数',
        }
    )

    confirm_password=forms.CharField(
        required=True,
        min_length=6,
        max_length=20,
        error_messages={
            'required': '密码不能为空',
            'min_length': '密码长度最少6位数',
        }
    )

    class Meta:
        model=User
        fields=['name','gender','birthday','date_joined','username','mobile','email',
                'department','post','superior','is_active','roles','password']
        error_messages={
            'name':{'required':'姓名不能为空'},
            'date_joined':{'required':"入职日期不能为空"},
            'username':{'required':'用户名不能为空'},
            'email':{'required':'邮箱不能为空'},
            'mobile':{
                'required':'手机号码不能为空',
                'max_length':'输入有效的手机号码',
                'min_length':'输入有效的手机号码'
            }
        }

    def clean(self):
        cleaned_data=super(UserCreateForm,self).clean()
        username=cleaned_data.get('username')
        mobile=cleaned_data.get('mobile')
        email=cleaned_data.get('email')
        password=cleaned_data.get('password')
        confirm_password=cleaned_data.get('confirm_password')

        if User.objects.filter(username=username).count():
            raise forms.ValidationError("用户名：{}已存在".format(username))

        if password!=confirm_password:
            raise forms.ValidationError('两次密码不一致')

        if User.objects.filter(mobile=mobile).count():
            raise forms.ValidationError("手机号：{}已存在".format(mobile))

        REGEX_MOBILE = "^1[3578]\d{9}$|^147\d{8}$|^176\d{8}$"
        #如果手机号为空或匹配不上正则就报错
        if mobile and not re.match(REGEX_MOBILE,mobile):
            raise forms.ValidationError('手机号码非法')

        if User.objects.filter(email=email).count():
            raise forms.ValidationError("邮箱：{}已存在".format(email))

class UserUpdateForm(forms.ModelForm):
    class Meta:
        model=User
        fields=[
            'name', 'gender', 'birthday', 'date_joined', 'username', 'mobile', 'email',
            'department', 'post', 'superior', 'is_active', 'roles'
        ]

class PasswordChangeForm(forms.Form):
    password = forms.CharField(
        required=True,
        min_length=6,
        max_length=20,
        error_messages={
            'required': '密码不能为空',
            'min_length': '密码长度最少6位数',
        }
    )

    confirm_password = forms.CharField(
        required=True,
        min_length=6,
        max_length=20,
        error_messages={
            'required': '确认密码不能为空',
            'min_length': '密码长度最少6位数',
        }
    )

    def clean(self):
        cleaned_data=super(PasswordChangeForm,self).clean()
        password=cleaned_data.get('password')
        confirm_password=cleaned_data.get('confirm_password')
        if password !=confirm_password:
            raise forms.ValidationError('两次密码不一致')

class MenuForm(forms.ModelForm):
    class Meta:
        model=Menu
        fields="__all__"

